#property copyright   "Copyright 2013,mingxinhe@gmail.com"
#property link "mingxinhe@gmail.com"

#define LONG          0
#define SHORT         1
#define ALL           2
#define RETRYCOUNT    10   
#define RETRYDELAY    500

extern string StochHistogram_Setting ="------StochHistogram Setting------";
extern bool   StochHistogram_OnOff = true;
extern string StochHistogram_Name = "StochHistogram";
extern string note1 = "First Stochastic";
extern int    StochPeriod1=14;
extern int    DPeriod1=3;
extern int    SlowingPeriod1=3;
extern int    StochHistogram_Shift = 0;

extern string MACD_Setting ="------MACD Setting------";
extern bool   MACD_OnOff = true;
extern string MACD_Name = "MACD_ColorHist_Alert  12  26  9   LA ";
extern int    FastEMA=8;
extern int    SlowEMA=21;
extern int    SignalSMA=8;
extern int    MACD_Shift = 0;

extern string CCI_Setting ="------CCI Setting------";
extern bool   CCI_OnOff = true;
extern string CCI_Name = "CCI - nrp & mtf advanced_alerts";
       string CCI_TimeFrame           = "current time frame";
extern int    CCI_Period           = 14;
extern int    CCI_Price            = PRICE_TYPICAL;
extern double CCI_OverSold            = -90;
extern double CCI_OverBought          = 90;
extern double CCI_SmoothLength        =    5;
extern double CCI_SmoothPhase         =    0;
extern int    CCI_Shift               =    0;

extern string laugerre_Setting ="------laugerre Setting------";
extern bool   laugerre_OnOff = true;
extern string laugerre_Name = "Laguerre-ACS1";
extern double gamma = 0.6;
extern int    MaxBars = 1000;
extern int    MA = 2;
extern int    laugerre_Shift = 0; 
extern double laugerre_HighLevel = 0.85; 
extern double laugerre_LowLevel = 0.15; 

extern string pbf_fast_3mas_Setting ="------pbf_fast_3mas Setting------";
extern bool   pbf_fast_3mas_OnOff = true;
extern string pbf_fast_3mas_Name = "PBF_Fast_3MAs";
extern int    PLength_Signal = 2;
extern int    PLength1 = 13;
extern int    PLength2 = 17;
extern int    pbf_fast_3mas_Shift = 0; 

extern string PBF_pb_direction_Setting ="------PBF_pb_direction Setting------";
extern bool   PBF_pb_direction_OnOff = true;
extern string PBF_pb_direction_Name = "PBF_PB_Direction";
extern int    PBF_pb_direction_Length = 21;
extern int    PBF_pb_direction_Shift = 0; 

extern string PBF_Squeeze_Setting ="------PBF_Squeeze Setting------";
extern bool   PBF_Squeeze_OnOff = true;
extern string PBF_Squeeze_Name = "PBF_Squeeze";
extern int    PBF_Length = 21;
extern int    PBF_Shift = 0; 

extern string PBF_Trend_Bars_V2_Setting ="------PBF_Trend_Bars_V2 Setting------";
extern bool   PBF_Trend_Bars_V2_OnOff = true;
extern string PBF_Trend_Bars_V2_Name = "PBF_Trend_Bars_V2";
extern int    PBF_Trend_Bars_V2_Length1 = 13;
extern int    PBF_Trend_Bars_V2_Length2 = 34;
extern int    PBF_Trend_Bars_V2_Length3 = 89;
extern int    PBF_Trend_Bars_V2_Shift = 0; 

extern string MA_Setting ="------3MA Setting------";
extern bool   MA_OnOff = true;
extern int    MA_Long_Period = 55;
extern int    MA_Middle_Period = 21;
extern int    MA_Short_Period = 13;
extern int    MA_Shift = 0;

extern string PBF_2ema_color_Setting ="------PBF_2ema Setting------";
extern bool   PBF_2ema_color_OnOff = true;
extern string PBF_2ema_color_Name = "PBF_2ema_color";
extern int    PBF_2ema_color_MA1_Period = 49;
extern int    PBF_2ema_color_MA2_Period = 89;
extern int    PBF_2ema_color_Shift = 0;

extern string ADX_coloured_Setting ="------ADX_coloured Setting------";
extern bool   ADX_coloured_OnOff = true;
extern string ADX_coloured_Name = "ADX_coloured";
extern int    ADXPeriod = 14;
extern int    colour_level = 20;
extern int    ADX_coloured_Shift = 0;

extern string SLTP_Setting ="------Stop Loss and Take Profit Setting------";
 bool   TrailingStop.OnOff = false;
 double TrailingStop.Pips = 30;
extern double TrailingStopStep.Pips = 1;
extern string InitialStop.IndicatorName = "ATR_Stop";
extern int    ATR_Stop_ATRPeriod = 10;
extern double ATR_Stop_Factor = 2;
extern int    ATR_Stop_Shift = 0;
extern double Security.StopLoss.Pips = 50;
extern int    TakeProfit.Radio = 2;
extern double ClosePercentAtFirstTakeProfit = 50;

extern string Other_Setting  = "------Other Setting-------";
extern double Risk.Percent.Of.Balance = 2;
extern bool   PopupAlert = true; 
extern bool   SoundAlert = true; 
extern string SoundFileName = "Alert.wav";
extern bool   ManualTrade = false;
extern bool   stealth = false; 
extern int    MagicNumber=-1;
extern int    Slippage = 3;

double dStopLoss;
int    MaxTrades = 1;
int digitFactor = 1;
double lastActTime;

int orders.total, orders.BUY, orders.SELL, orders.BUYLIMIT, orders.BUYSTOP, orders.SELLLIMIT, orders.SELLSTOP;

int lotDigits; 
     

int init()
{    
    if(Bid<10 && Digits==5) { digitFactor=10;}
    if(Bid<10 && Digits==4) { digitFactor= 1;}

    if(Bid>10 && Digits==3) { digitFactor=10;}
    if(Bid>10 && Digits==2) { digitFactor= 1;}   
    
    Security.StopLoss.Pips *= digitFactor; 
    TrailingStop.Pips *= digitFactor; 
    TrailingStopStep.Pips *= digitFactor; 
    
    if (MagicNumber < 0)
    {
        sub_magicnumber();
    }
    
    lotDigits = MathLog(MarketInfo(Symbol(), MODE_LOTSTEP))/MathLog(0.1); 
}

int deinit()
{
   return(0);
}

int start()
{ 
    if (StochHistogram_OnOff)
    {
        double StochHistogram = iCustom(NULL, 0, StochHistogram_Name, note1, StochPeriod1, DPeriod1, SlowingPeriod1, 2, StochHistogram_Shift);
        double StochHistogram_Pre = iCustom(NULL, 0, StochHistogram_Name, note1, StochPeriod1, DPeriod1, SlowingPeriod1, 2, StochHistogram_Shift+1);
        //Print("StochHistogram:", StochHistogram, " StochHistogram_Pre:",StochHistogram_Pre);
    }
    
    if (MACD_OnOff)
    {
        double MACD_Green = iCustom(NULL, 0, MACD_Name, False, False, FastEMA, SlowEMA, 2, MACD_Shift);
        double MACD_Green_Pre = iCustom(NULL, 0, MACD_Name, False, False, FastEMA, SlowEMA, 2, MACD_Shift+1);
        double MACD_Red = iCustom(NULL, 0, MACD_Name, False, False, FastEMA, SlowEMA, 3, MACD_Shift);
        double MACD_Red_Pre = iCustom(NULL, 0, MACD_Name, False, False, FastEMA, SlowEMA, 3, MACD_Shift+1);
        //Print("MACD_Green:",MACD_Green, " MACD_Green_Pre:",MACD_Green_Pre," MACD_Red:",MACD_Red, " MACD_Red_Pre:",MACD_Red_Pre);
    }
    
    if (CCI_OnOff)
    {
        double CCI = iCustom(NULL, 0, CCI_Name, CCI_TimeFrame, CCI_Period, CCI_Price, CCI_OverSold, CCI_OverBought, CCI_SmoothLength, CCI_SmoothPhase, CCI_Shift);
        double CCI_Pre = iCustom(NULL, 0, CCI_Name, CCI_TimeFrame, CCI_Period, CCI_Price, CCI_OverSold, CCI_OverBought, CCI_SmoothLength, CCI_SmoothPhase, CCI_Shift+1);
    }    

    if (laugerre_OnOff)
    {
        double laugerre = iCustom(NULL, 0, laugerre_Name, gamma, MaxBars, MA, 0, laugerre_Shift);
        double laugerre_Pre = iCustom(NULL, 0, laugerre_Name, gamma, MaxBars, MA, 0, laugerre_Shift+1);
    }
    
    if (pbf_fast_3mas_OnOff)
    {
         double pbf_fast_3mas_0 = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 0, pbf_fast_3mas_Shift);
         double pbf_fast_3mas_1 = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 1, pbf_fast_3mas_Shift);
         double pbf_fast_3mas_2 = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 2, pbf_fast_3mas_Shift);
         double pbf_fast_3mas_3 = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 3, pbf_fast_3mas_Shift);
         double pbf_fast_3mas_4 = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 4, pbf_fast_3mas_Shift);
    
         double pbf_fast_3mas_0_Pre = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 0, pbf_fast_3mas_Shift+1);
         double pbf_fast_3mas_1_Pre = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 1, pbf_fast_3mas_Shift+1);
         double pbf_fast_3mas_2_Pre = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 2, pbf_fast_3mas_Shift+1);
         double pbf_fast_3mas_3_Pre = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 3, pbf_fast_3mas_Shift+1);
         double pbf_fast_3mas_4_Pre = iCustom(NULL, 0, pbf_fast_3mas_Name, PLength_Signal, PLength1, PLength2, 4, pbf_fast_3mas_Shift+1);
    }
    
    if (PBF_pb_direction_OnOff)
    {
         double PBF_pb_direction_2 = iCustom(NULL, 0, PBF_pb_direction_Name, PBF_pb_direction_Length, 2, PBF_pb_direction_Shift);
         double PBF_pb_direction_3 = iCustom(NULL, 0, PBF_pb_direction_Name, PBF_pb_direction_Length, 3, PBF_pb_direction_Shift);
    
         double PBF_pb_direction_2_Pre = iCustom(NULL, 0, PBF_pb_direction_Name, PBF_pb_direction_Length, 2, PBF_pb_direction_Shift+1);
         double PBF_pb_direction_3_Pre = iCustom(NULL, 0, PBF_pb_direction_Name, PBF_pb_direction_Length, 3, PBF_pb_direction_Shift+1);
    }
    
    if (PBF_Squeeze_OnOff)
    {
         double PBF_Squeeze_0 = iCustom(NULL, 0, PBF_Squeeze_Name, PBF_Length, 0, PBF_Shift);
         double PBF_Squeeze_0_Pre = iCustom(NULL, 0, PBF_Squeeze_Name, PBF_Length, 0, PBF_Shift+1);
         
         double PBF_Squeeze_1 = iCustom(NULL, 0, PBF_Squeeze_Name, PBF_Length, 1, PBF_Shift);
         double PBF_Squeeze_1_Pre = iCustom(NULL, 0, PBF_Squeeze_Name, PBF_Length, 1, PBF_Shift+1);
    }
    
    if (PBF_Trend_Bars_V2_OnOff)
    {
         double PBF_Trend_Bars_V2_0 = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 0, PBF_Trend_Bars_V2_Shift);
         double PBF_Trend_Bars_V2_2 = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 2, PBF_Trend_Bars_V2_Shift);
         double PBF_Trend_Bars_V2_4 = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 4, PBF_Trend_Bars_V2_Shift);        
                                         
         double PBF_Trend_Bars_V2_0_Pre = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 0, PBF_Trend_Bars_V2_Shift+1);
         double PBF_Trend_Bars_V2_2_Pre = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 2, PBF_Trend_Bars_V2_Shift+1);
         double PBF_Trend_Bars_V2_4_Pre = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                         PBF_Trend_Bars_V2_Length3, 4, PBF_Trend_Bars_V2_Shift+1);    
          
         double PBF_Trend_Bars_V2_1 = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 1, PBF_Trend_Bars_V2_Shift);
         double PBF_Trend_Bars_V2_3 = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 3, PBF_Trend_Bars_V2_Shift);
         double PBF_Trend_Bars_V2_5 = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 5, PBF_Trend_Bars_V2_Shift);        
                                         
         double PBF_Trend_Bars_V2_1_Pre = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 1, PBF_Trend_Bars_V2_Shift+1);
         double PBF_Trend_Bars_V2_3_Pre = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                              PBF_Trend_Bars_V2_Length3, 3, PBF_Trend_Bars_V2_Shift+1);
         double PBF_Trend_Bars_V2_5_Pre = iCustom(NULL, 0, PBF_Trend_Bars_V2_Name, PBF_Trend_Bars_V2_Length1, PBF_Trend_Bars_V2_Length2, 
                                         PBF_Trend_Bars_V2_Length3, 5, PBF_Trend_Bars_V2_Shift+1);     
                                         
         //Print("PBF_Trend_Bars_V2_0:",PBF_Trend_Bars_V2_0, " PBF_Trend_Bars_V2_2:",PBF_Trend_Bars_V2_2, " PBF_Trend_Bars_V2_4:",PBF_Trend_Bars_V2_4,
         //      " PBF_Trend_Bars_V2_0_Pre:",PBF_Trend_Bars_V2_0_Pre, " PBF_Trend_Bars_V2_2_Pre:",PBF_Trend_Bars_V2_2_Pre, " PBF_Trend_Bars_V2_4_Pre:",PBF_Trend_Bars_V2_4_Pre,
         //      " PBF_Trend_Bars_V2_1:",PBF_Trend_Bars_V2_1, " PBF_Trend_Bars_V2_3:",PBF_Trend_Bars_V2_3, " PBF_Trend_Bars_V2_5:",PBF_Trend_Bars_V2_5,
          //     " PBF_Trend_Bars_V2_1_Pre:",PBF_Trend_Bars_V2_1_Pre, " PBF_Trend_Bars_V2_3_Pre:",PBF_Trend_Bars_V2_3_Pre, " PBF_Trend_Bars_V2_5_Pre:",PBF_Trend_Bars_V2_5_Pre);                                                               
    }
    
    if (MA_OnOff)
    {
         double MA_Long = iMA(NULL, 0, MA_Long_Period, 0, MODE_EMA, PRICE_CLOSE, MA_Shift);
         double MA_Middle = iMA(NULL, 0, MA_Middle_Period, 0, MODE_EMA, PRICE_CLOSE, MA_Shift);
         double MA_Short = iMA(NULL, 0, MA_Short_Period, 0, MODE_EMA, PRICE_CLOSE, MA_Shift);
    
         double MA_Long_Pre = iMA(NULL, 0, MA_Long_Period, 0, MODE_EMA, PRICE_CLOSE, MA_Shift+1);
         double MA_Middle_Pre = iMA(NULL, 0, MA_Middle_Period, 0, MODE_EMA, PRICE_CLOSE, MA_Shift+1);
         double MA_Short_Pre = iMA(NULL, 0, MA_Short_Period, 0, MODE_EMA, PRICE_CLOSE, MA_Shift+1);
         
         //Print("MA_Long:",MA_Long, " MA_Middle:",MA_Middle, " MA_Short:",MA_Short, 
         //      " MA_Long_Pre:",MA_Long_Pre, " MA_Middle_Pre:",MA_Middle_Pre, " MA_Short_Pre:",MA_Short_Pre);
    }
    
    if (PBF_2ema_color_OnOff)
    {
         double PBF_2ema_color_0 = iCustom(NULL, 0, PBF_2ema_color_Name, PBF_2ema_color_MA1_Period, PBF_2ema_color_MA2_Period, 0, PBF_2ema_color_Shift); 
         double PBF_2ema_color_0_Pre = iCustom(NULL, 0, PBF_2ema_color_Name, PBF_2ema_color_MA1_Period, PBF_2ema_color_MA2_Period, 0, PBF_2ema_color_Shift+1); 
         
         double PBF_2ema_color_2 = iCustom(NULL, 0, PBF_2ema_color_Name, PBF_2ema_color_MA1_Period, PBF_2ema_color_MA2_Period, 2, PBF_2ema_color_Shift); 
         double PBF_2ema_color_2_Pre = iCustom(NULL, 0, PBF_2ema_color_Name, PBF_2ema_color_MA1_Period, PBF_2ema_color_MA2_Period, 2, PBF_2ema_color_Shift+1); 
         
         //Print("PBF_2ema_color_0:",PBF_2ema_color_0, " PBF_2ema_color_0_Pre:",PBF_2ema_color_0_Pre, " PBF_2ema_color_2:",PBF_2ema_color_2, " PBF_2ema_color_2_Pre:",PBF_2ema_color_2_Pre);
    }
    
    if (ADX_coloured_OnOff)
    {
         double ADX_coloured_3 = iCustom(NULL, 0, ADX_coloured_Name, ADXPeriod, ADXPeriod, 3, ADX_coloured_Shift);
         double ADX_coloured_3_Pre = iCustom(NULL, 0, ADX_coloured_Name, ADXPeriod, ADXPeriod, 3, ADX_coloured_Shift+1);
         
         double ADX_coloured_4 = iCustom(NULL, 0, ADX_coloured_Name, ADXPeriod, ADXPeriod, 4, ADX_coloured_Shift);
         double ADX_coloured_4_Pre = iCustom(NULL, 0, ADX_coloured_Name, ADXPeriod, ADXPeriod, 4, ADX_coloured_Shift+1);
         
         //Print("ADX_coloured_3:",ADX_coloured_3, " ADX_coloured_3_Pre:",ADX_coloured_3_Pre, " ADX_coloured_4:",ADX_coloured_4, " ADX_coloured_4_Pre:",ADX_coloured_4_Pre);
    }
    
    bool canBuyNow = (StochHistogram_OnOff == false || StochHistogram > 0) && 
        (MACD_OnOff == false || (MACD_Green > 0 && MACD_Green != EMPTY_VALUE)) && 
        (CCI_OnOff == false || CCI > CCI_OverBought) && (laugerre_OnOff == false || laugerre > laugerre_LowLevel) && 
        (pbf_fast_3mas_OnOff == false || 
          (pbf_fast_3mas_2 > 0 && pbf_fast_3mas_2 != EMPTY_VALUE && Close[pbf_fast_3mas_Shift] > Open[pbf_fast_3mas_Shift] &&
           (pbf_fast_3mas_0 == EMPTY_VALUE || Low[pbf_fast_3mas_Shift] > pbf_fast_3mas_0) &&
           (pbf_fast_3mas_1 == EMPTY_VALUE || Low[pbf_fast_3mas_Shift] > pbf_fast_3mas_1) &&
           (pbf_fast_3mas_2 == EMPTY_VALUE || Low[pbf_fast_3mas_Shift] > pbf_fast_3mas_2) &&
           (pbf_fast_3mas_3 == EMPTY_VALUE || Low[pbf_fast_3mas_Shift] > pbf_fast_3mas_3) &&
           (pbf_fast_3mas_4 == EMPTY_VALUE || Low[pbf_fast_3mas_Shift] > pbf_fast_3mas_4))) &&
        (PBF_pb_direction_OnOff == false || PBF_pb_direction_2 > PBF_pb_direction_3) &&   
        (PBF_Squeeze_OnOff == false || (PBF_Squeeze_0 > 0 && PBF_Squeeze_0 != EMPTY_VALUE)) &&
        (PBF_Trend_Bars_V2_OnOff == false || (PBF_Trend_Bars_V2_0 == 35 && PBF_Trend_Bars_V2_2 == 0 && PBF_Trend_Bars_V2_4 == -35)) &&
        (MA_OnOff == false || (Low[MA_Shift] > MA_Short && MA_Short > MA_Middle && MA_Middle > MA_Long)) &&
        (PBF_2ema_color_OnOff == false || (PBF_2ema_color_0 > 0 && PBF_2ema_color_0 != EMPTY_VALUE) &&
        (ADX_coloured_OnOff == false || (ADX_coloured_3 > 0 && ADX_coloured_3 != EMPTY_VALUE)));
        
        
    
    //Print("StochHistogram:", StochHistogram);
    bool canBuyPre = (StochHistogram_OnOff == false || StochHistogram_Pre > 0) && 
        (MACD_OnOff == false || (MACD_Green_Pre > 0 && MACD_Green_Pre != EMPTY_VALUE)) && 
        (CCI_OnOff == false || CCI_Pre > CCI_OverBought) && (laugerre_OnOff == false || laugerre_Pre > laugerre_LowLevel) && 
        (pbf_fast_3mas_OnOff == false || 
          (pbf_fast_3mas_2_Pre > 0 && pbf_fast_3mas_2_Pre != EMPTY_VALUE && Close[pbf_fast_3mas_Shift+1] > Open[pbf_fast_3mas_Shift+1] &&
           (pbf_fast_3mas_0_Pre == EMPTY_VALUE || Low[pbf_fast_3mas_Shift+1] > pbf_fast_3mas_0_Pre) &&
           (pbf_fast_3mas_1_Pre == EMPTY_VALUE || Low[pbf_fast_3mas_Shift+1] > pbf_fast_3mas_1_Pre) &&
           (pbf_fast_3mas_2_Pre == EMPTY_VALUE || Low[pbf_fast_3mas_Shift+1] > pbf_fast_3mas_2_Pre) &&
           (pbf_fast_3mas_3_Pre == EMPTY_VALUE || Low[pbf_fast_3mas_Shift+1] > pbf_fast_3mas_3_Pre) &&
           (pbf_fast_3mas_4_Pre == EMPTY_VALUE || Low[pbf_fast_3mas_Shift+1] > pbf_fast_3mas_4_Pre))) &&
        (PBF_pb_direction_OnOff == false || PBF_pb_direction_2_Pre > PBF_pb_direction_3_Pre) &&    
        (PBF_Squeeze_OnOff == false || (PBF_Squeeze_0_Pre > 0 && PBF_Squeeze_0_Pre != EMPTY_VALUE)) &&            
        (PBF_Trend_Bars_V2_OnOff == false || (PBF_Trend_Bars_V2_0_Pre == 35 && PBF_Trend_Bars_V2_2_Pre == 0 && PBF_Trend_Bars_V2_4_Pre == -35)) &&
        (MA_OnOff == false || (Low[MA_Shift+1] > MA_Short_Pre && MA_Short_Pre > MA_Middle_Pre && MA_Middle_Pre > MA_Long_Pre)) &&
        (PBF_2ema_color_OnOff == false || (PBF_2ema_color_0_Pre > 0 && PBF_2ema_color_0_Pre != EMPTY_VALUE) &&
        (ADX_coloured_OnOff == false || (ADX_coloured_3_Pre > 0 && ADX_coloured_3_Pre != EMPTY_VALUE)));         
    
    bool canSellNow = (StochHistogram_OnOff == false || StochHistogram < 0) && 
        (MACD_OnOff == false || (MACD_Red < 0 && MACD_Red != EMPTY_VALUE)) && 
        (CCI_OnOff == false || CCI < CCI_OverSold) && (laugerre_OnOff == false || laugerre < laugerre_HighLevel) && 
        (pbf_fast_3mas_OnOff == false || 
          (pbf_fast_3mas_3 > 0 && pbf_fast_3mas_3 != EMPTY_VALUE && Close[pbf_fast_3mas_Shift] < Open[pbf_fast_3mas_Shift] &&
           (pbf_fast_3mas_0 == EMPTY_VALUE || High[pbf_fast_3mas_Shift] < pbf_fast_3mas_0) &&
           (pbf_fast_3mas_1 == EMPTY_VALUE || High[pbf_fast_3mas_Shift] < pbf_fast_3mas_1) &&
           (pbf_fast_3mas_2 == EMPTY_VALUE || High[pbf_fast_3mas_Shift] < pbf_fast_3mas_2) &&
           (pbf_fast_3mas_3 == EMPTY_VALUE || High[pbf_fast_3mas_Shift] < pbf_fast_3mas_3) &&
           (pbf_fast_3mas_4 == EMPTY_VALUE || High[pbf_fast_3mas_Shift] < pbf_fast_3mas_4))) &&
        (PBF_pb_direction_OnOff == false || PBF_pb_direction_2 < PBF_pb_direction_3) &&   
        (PBF_Squeeze_OnOff == false || (PBF_Squeeze_1 < 0 && PBF_Squeeze_1 != EMPTY_VALUE)) &&        
        (PBF_Trend_Bars_V2_OnOff == false || (PBF_Trend_Bars_V2_1 == 35 && PBF_Trend_Bars_V2_3 == 0 && PBF_Trend_Bars_V2_5 == -35)) &&
        (MA_OnOff == false || (High[MA_Shift] < MA_Short && MA_Short < MA_Middle && MA_Middle < MA_Long)) &&
        (PBF_2ema_color_OnOff == false || (PBF_2ema_color_2 > 0 && PBF_2ema_color_2 != EMPTY_VALUE) &&
        (ADX_coloured_OnOff == false || (ADX_coloured_4 > 0 && ADX_coloured_4 != EMPTY_VALUE)));        
    
    //Print("StochHistogram:", StochHistogram);
    bool canSellPre = (StochHistogram_OnOff == false || StochHistogram_Pre < 0) && 
        (MACD_OnOff == false || (MACD_Red_Pre < 0 && MACD_Red_Pre != EMPTY_VALUE)) && 
        (CCI_OnOff == false || CCI_Pre < CCI_OverSold) && (laugerre_OnOff == false || laugerre_Pre < laugerre_HighLevel) && 
        (pbf_fast_3mas_OnOff == false || 
          (pbf_fast_3mas_3_Pre > 0 && pbf_fast_3mas_3_Pre != EMPTY_VALUE && Close[pbf_fast_3mas_Shift+1] < Open[pbf_fast_3mas_Shift+1] &&
           (pbf_fast_3mas_0_Pre == EMPTY_VALUE || High[pbf_fast_3mas_Shift+1] < pbf_fast_3mas_0_Pre) &&
           (pbf_fast_3mas_1_Pre == EMPTY_VALUE || High[pbf_fast_3mas_Shift+1] < pbf_fast_3mas_1_Pre) &&
           (pbf_fast_3mas_2_Pre == EMPTY_VALUE || High[pbf_fast_3mas_Shift+1] < pbf_fast_3mas_2_Pre) &&
           (pbf_fast_3mas_3_Pre == EMPTY_VALUE || High[pbf_fast_3mas_Shift+1] < pbf_fast_3mas_3_Pre) &&
           (pbf_fast_3mas_4_Pre == EMPTY_VALUE || High[pbf_fast_3mas_Shift+1] < pbf_fast_3mas_4_Pre))) &&
        (PBF_pb_direction_OnOff == false || PBF_pb_direction_2_Pre < PBF_pb_direction_3_Pre) &&    
        (PBF_Squeeze_OnOff == false || (PBF_Squeeze_1_Pre < 0 && PBF_Squeeze_1_Pre != EMPTY_VALUE)) &&            
        (PBF_Trend_Bars_V2_OnOff == false || (PBF_Trend_Bars_V2_1_Pre == 35 && PBF_Trend_Bars_V2_3_Pre == 0 && PBF_Trend_Bars_V2_5_Pre == -35)) &&
        (MA_OnOff == false || (High[MA_Shift+1] < MA_Short_Pre && MA_Short_Pre < MA_Middle_Pre && MA_Middle_Pre < MA_Long_Pre)) &&
        (PBF_2ema_color_OnOff == false || (PBF_2ema_color_2_Pre > 0 && PBF_2ema_color_2_Pre != EMPTY_VALUE) &&
        (ADX_coloured_OnOff == false || (ADX_coloured_4_Pre > 0 && ADX_coloured_4_Pre != EMPTY_VALUE)));         
    
    
    //Print("canBuyNow:",canBuyNow, " canBuyPre:",canBuyPre, " canSellNow:",canSellNow, " canSellPre:",canSellPre);
    
    if (canBuyNow && !canBuyPre && Time[0] > lastActTime)
    {
        CountOrders();
        
        if (orders.total < MaxTrades)
        {
            if (!ManualTrade)
            {
                if (DoOpenOrder(Symbol(), LONG, Blue, "") > 0)
                {
                    lastActTime = Time[0];
                }
            }
            
            doAlert("Buy Signal Occurs!");
        }
    }
    
    if (canSellNow && !canSellPre && Time[0] > lastActTime)
    {
        CountOrders();
        
        if (orders.total < MaxTrades)
        {
            if (!ManualTrade)
            {
                if (DoOpenOrder(Symbol(), SHORT, Red, "") > 0)
                {
                    lastActTime = Time[0];
                }
            }
            
            doAlert("Sell Signal Occurs!");
        }
    }    
    
    loopOrders();
}

double getLots(double openPrice, double stopLossPrice)
{    
    double lot = AccountBalance()*Risk.Percent.Of.Balance/100/
         (MathAbs(openPrice - stopLossPrice)*MarketInfo(Symbol(), MODE_TICKVALUE) / MarketInfo(Symbol(), MODE_TICKSIZE));
    
    return (NormalizeDouble(lot, lotDigits));
}

/*
void trailingStopOrders() 
{                
    for (int i = OrdersTotal() - 1; i >= 0; i--) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;             
      if(OrderMagicNumber() == MagicNumber)
      {
         string symbolName = OrderSymbol();
         double dDigits = MarketInfo(symbolName, MODE_DIGITS);
         double dPoint = MarketInfo(symbolName, MODE_POINT);   

         double dAsk = MarketInfo(symbolName, MODE_ASK);
         double dBid = MarketInfo(symbolName, MODE_BID);
             
         if ((OrderType() == OP_BUY || OrderType() == OP_SELL) && TrailingStop.Pips > 0 && TrailingStopStep.Pips > 0)
         {    
            if (OrderType() == OP_BUY) 
            {       
               double SL = dBid - TrailingStop.Pips*dPoint;            
               if (dBid-OrderOpenPrice()>=TrailingStop.Pips*dPoint && SL >= OrderStopLoss() + TrailingStopStep.Pips*dPoint) 
               {                 
                   modifyOrderByPrice(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit());
               }
            }

            if (OrderType() == OP_SELL) 
            {                
               SL = dAsk + TrailingStop.Pips*dPoint;
               if (OrderOpenPrice()-dAsk>=TrailingStop.Pips*dPoint && (SL <= OrderStopLoss() - TrailingStopStep.Pips*dPoint|| OrderStopLoss() == 0)) 
               {               
                   modifyOrderByPrice(OrderTicket(), OrderOpenPrice(), SL, OrderTakeProfit());
               }
            }
         }
      }
   }
}
*/

void loopOrders() 
{                
    for (int i = OrdersTotal() - 1; i >= 0; i--) 
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;             
        if(OrderMagicNumber() == MagicNumber)
        {
            string symbolName = OrderSymbol();
            double dDigits = MarketInfo(symbolName, MODE_DIGITS);
            double dPoint = MarketInfo(symbolName, MODE_POINT);   

            double dAsk = MarketInfo(symbolName, MODE_ASK);
            double dBid = MarketInfo(symbolName, MODE_BID);

            if ((OrderType() == OP_BUY || OrderType() == OP_SELL))
            {    
                if (OrderTakeProfit() > 0)
                {
                    if (OrderType() == OP_BUY) 
                    {
                        if (stealth && dBid <= dStopLoss)
                        {
                            if (Exit(OrderTicket(), symbolName, LONG, OrderLots(), Blue))
                            {
                                dStopLoss = 0;
                                continue;
                            }
                        }

                        if (StrToDouble(OrderComment()) == OrderLots())
                        {
                            if (dBid - OrderOpenPrice() >= (OrderTakeProfit() - OrderOpenPrice())/2)
                            {
                                if (stealth)
                                {
                                    Exit(OrderTicket(), symbolName, LONG, NormalizeDouble(OrderLots()*ClosePercentAtFirstTakeProfit/100, lotDigits), Blue);
                                    dStopLoss = OrderOpenPrice();
                                }
                                else
                                {
                                    if (modifyOrderByPrice(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit()))
                                    {
                                        Exit(OrderTicket(), symbolName, LONG, NormalizeDouble(OrderLots()*ClosePercentAtFirstTakeProfit/100, lotDigits), Blue);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (stealth)
                            {                                
                                if (dStopLoss > 0 && dBid - dStopLoss >= (OrderTakeProfit() - OrderOpenPrice())/2 + TrailingStopStep.Pips*dPoint)
                                {
                                    dStopLoss = dBid - (OrderTakeProfit() - OrderOpenPrice())/2;
                                }
                            }
                            else if (dBid - OrderStopLoss() >= (OrderTakeProfit() - OrderOpenPrice())/2 + TrailingStopStep.Pips*dPoint)
                            {                            
                                double sl = dBid - (OrderTakeProfit() - OrderOpenPrice())/2;
                                modifyOrderByPrice(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit());
                            }
                        }
                    } 
                    else if (OrderType() == OP_SELL) 
                    {
                        if (stealth && dAsk >= dStopLoss && dStopLoss > 0)
                        {
                            if (Exit(OrderTicket(), symbolName, SHORT, OrderLots(), Red))
                            {
                                dStopLoss = 0;
                                continue;
                            }
                        }
                    
                        if (StrToDouble(OrderComment()) == OrderLots())
                        {
                            if (OrderOpenPrice() - dAsk >= (OrderOpenPrice() - OrderTakeProfit())/2)
                            {
                                if (stealth)
                                {
                                    Exit(OrderTicket(), symbolName, SHORT, NormalizeDouble(OrderLots()*ClosePercentAtFirstTakeProfit/100, lotDigits), Red);
                                    dStopLoss = OrderOpenPrice();
                                }
                                else
                                {
                                    if (modifyOrderByPrice(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit()))
                                    {
                                        Exit(OrderTicket(), symbolName, SHORT, NormalizeDouble(OrderLots()*ClosePercentAtFirstTakeProfit/100, lotDigits), Red);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (stealth)
                            {
                                if (dStopLoss > 0 && dStopLoss - dAsk >= (OrderTakeProfit() - OrderOpenPrice())/2 + TrailingStopStep.Pips*dPoint)
                                {
                                    dStopLoss = dAsk + (OrderTakeProfit() - OrderOpenPrice())/2;
                                }
                            }
                            else if (OrderStopLoss() - dAsk >= (OrderTakeProfit() - OrderOpenPrice())/2 + TrailingStopStep.Pips*dPoint)
                            {
                                sl = dAsk + (OrderTakeProfit() - OrderOpenPrice())/2;
                                modifyOrderByPrice(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit());
                            }
                        }
                    }
                }
            }
        }
    }
}

//Count Orders and calculate the group's average entry point & spread
int CountOrders() 
{
   orders.BUY = 0;
   orders.SELL = 0;
   orders.BUYLIMIT = 0;
   orders.BUYSTOP = 0;
   orders.SELLLIMIT = 0;
   orders.SELLSTOP = 0;
   
   for( int i = OrdersTotal() - 1; i >= 0; i--) 
   {   
      if (!OrderSelect( i, SELECT_BY_POS)) continue;
      
      if( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber ) 
      {                       
         if( OrderType() == OP_BUY ) 
         { 
             orders.BUY++; 
         }
         if( OrderType() == OP_SELL ) 
         { 
             orders.SELL++; 
         }
         if( OrderType() == OP_BUYLIMIT ) 
         { 
             orders.BUYLIMIT++; 
         }
         if( OrderType() == OP_BUYSTOP ) 
         { 
             orders.BUYSTOP++; 
         }
         if( OrderType() == OP_SELLLIMIT ) 
         { 
             orders.SELLLIMIT++; 
         }
         if( OrderType() == OP_SELLSTOP ) 
         { 
             orders.SELLSTOP++; 
         }
      }
   }
   
   orders.total = orders.BUY + orders.SELL;
   
   return(orders.total);

}

//open an order, if failed try again with RETRYCOUNT times
int DoOpenOrder(string symbolName, int dir, color clr, string comment = "")  {

   double entryPrice;

   int ticket = 0;   

   string info;   
   
   for (int i=0; i<RETRYCOUNT; i++) {
     for (int j=0; (j<50) && IsTradeContextBusy(); j++)
         Sleep(100);
      RefreshRates();   
      
      double dAsk = MarketInfo(symbolName, MODE_ASK);
      double dBid = MarketInfo(symbolName, MODE_BID);
      double dPoint = MarketInfo(symbolName, MODE_POINT);
      double dDigits = MarketInfo(symbolName, MODE_DIGITS);
      double dStopLevel = MarketInfo(symbolName, MODE_STOPLEVEL)*dPoint;
      double dSpread = MarketInfo(symbolName, MODE_SPREAD)*dPoint;


      switch(dir)  {
         case LONG:               
               entryPrice = NormalizeDouble(dAsk, dDigits);
               
               double sl = iCustom(NULL, 0, InitialStop.IndicatorName, ATR_Stop_ATRPeriod, ATR_Stop_Factor, 0, ATR_Stop_Shift);        
                if (sl > entryPrice - dStopLevel  || sl <= 0) { sl = entryPrice - Security.StopLoss.Pips*dPoint; }   
                sl = NormalizeDouble(sl, dDigits);
                
                double lots = getLots(dAsk, sl);
                
                string lotStr = DoubleToStr(lots, lotDigits);
                
                double tp = entryPrice + (entryPrice - sl)*TakeProfit.Radio + dSpread;
                tp = NormalizeDouble(tp, dDigits);                               
                
                info = "Type: " + dir + ", \nentryPrice: " + DoubleToStr(entryPrice, dDigits) 
                   + ", \nLots " + DoubleToStr(lots, 2) + " , \nStop: " + DoubleToStr(sl, dDigits)  
                   + ", \nTP: " + DoubleToStr(tp, dDigits);
                Print(info);
                Comment(info);   
                
                if (stealth)
                {
                    dStopLoss = sl;
                    sl = 0;
                }             
               
                ticket = OrderSend(symbolName, OP_BUY, lots, entryPrice, Slippage, sl, tp, lotStr, MagicNumber, 0, clr);
                if (ticket < 0)  
                {
                    ticket = OrderSend(symbolName, OP_BUY, lots, entryPrice, Slippage, 0, 0, lotStr, MagicNumber, 0, clr);                    
                }
                if (ticket > 0)  
                {
                     if(OrderSelect(ticket, SELECT_BY_TICKET))
                     {
                         modifyOrderByPrice(ticket, OrderOpenPrice(), sl, tp);
                     }
                }
                
                break;

         case SHORT:
              entryPrice = NormalizeDouble(dBid, dDigits);
              
              sl = iCustom(NULL, 0, InitialStop.IndicatorName, ATR_Stop_ATRPeriod, ATR_Stop_Factor, 1, ATR_Stop_Shift);
              if (sl < entryPrice + dStopLevel  || sl <= 0) { sl = entryPrice + Security.StopLoss.Pips*dPoint; } 
              sl = NormalizeDouble(sl, dDigits);
              
              lots = getLots(dAsk, sl);
              lotStr = DoubleToStr(lots, lotDigits);
              
              tp = entryPrice - (sl - entryPrice)*TakeProfit.Radio - dSpread;
              tp = NormalizeDouble(tp, dDigits); 
                
                info = "Type: " + dir + ", \nentryPrice: " + DoubleToStr(entryPrice, dDigits)
                   + ", \nLots " + DoubleToStr(lots, 2) + " , \nStop: " + DoubleToStr(sl, dDigits)  
                   + ", \nTP: " + DoubleToStr(tp, dDigits);
              Print(info);
              Comment(info);
              
                if (stealth)
                {
                    dStopLoss = sl;
                    sl = 0;
                } 
          
                ticket = OrderSend(symbolName, OP_SELL, lots, entryPrice, Slippage, sl, tp, lotStr, MagicNumber, 0, clr);     
                if (ticket < 0)  
                {
                    ticket = OrderSend(symbolName, OP_SELL, lots, entryPrice, Slippage, 0, 0, lotStr, MagicNumber, 0, clr);                    
                    
                }   
                if (ticket > 0)  
                {
                   if(OrderSelect(ticket, SELECT_BY_TICKET))
                   {
                       modifyOrderByPrice(ticket, OrderOpenPrice(), sl, tp);
                   }
                }                      
                break;
      }
           
      if( ticket > 0 ) { return( ticket ); }
      else {
         Print("DoOpenOrder: error \'"+ErrorDescription(GetLastError())+"\' when opening entry order");
         Sleep(RETRYDELAY);      
      }
      
   }
      
   return( false );
}

//close an order with specifed ticket, if failed try again with RETRYCOUNT times
bool Exit(int ticket, string symbolName, int dir, double volume, color clr)  {
    int i, j, cmd;
    double prc, sl, tp, lots;
    string cmt;

    bool closed;

    Print("Exit("+dir+","+DoubleToStr(volume,3)+")");

    for (i=0; i<RETRYCOUNT; i++) {
        for (j=0; (j<50) && IsTradeContextBusy(); j++)
            Sleep(100);
        RefreshRates();

        if (dir == OP_BUY) {
            prc = MarketInfo(symbolName, MODE_BID);
        }
        if (dir == OP_SELL) {
            prc = MarketInfo(symbolName, MODE_ASK);
       }
        Print("Exit: prc="+DoubleToStr(prc,MarketInfo(symbolName, MODE_DIGITS)));

        closed = OrderClose(ticket,volume,prc,Slippage,clr);
        if (closed) {
            Print("Trade closed");

            return (true);
        }

        Print("Exit: error \'"+ErrorDescription(GetLastError())+"\' when exiting with "+DoubleToStr(volume,3)+" @"+DoubleToStr(prc,MarketInfo(symbolName, MODE_DIGITS)));
        Sleep(RETRYDELAY);
    }

    Print("Exit: can\'t enter after "+RETRYCOUNT+" retries");
    return (false);
}

//  Magic Number - calculated from a sum of account number and ASCII-codes from currency pair                                                                           
int sub_magicnumber()
{
     string local_currpair = Symbol();
     int local_length = StringLen (local_currpair);
     int local_asciisum = 0;
     int local_counter;

     for (local_counter = 0; local_counter < local_length -1; local_counter++)
        local_asciisum += StringGetChar (local_currpair, local_counter);
     MagicNumber = AccountNumber() + local_asciisum;   
}

//modify an order with specified ticket, if failed try again with RETRYCOUNT times
bool modifyOrderByPrice(int ticket, double price, double stopLoss, double takeProfit)  
{
   bool retVal = true;     
   
   if(!OrderSelect(ticket, SELECT_BY_TICKET)) return (false);
   
   for (int i=0; i<RETRYCOUNT; i++) 
   {
     for (int j=0; (j<50) && IsTradeContextBusy(); j++)
        Sleep(100);
      RefreshRates();                        

       if (MathAbs(OrderStopLoss() - stopLoss) > Point || MathAbs(OrderTakeProfit() - takeProfit) > Point)
       {              
           retVal = OrderModify(ticket, price, stopLoss, takeProfit, 0);
       }
         
      if(retVal) { return( true ); } 
      else {
         Print("DoModifyOrder: error \'"+ErrorDescription(GetLastError())+"\' when modifying order SL:", stopLoss, " TP:", takeProfit, " OrderOpenPrice:",OrderOpenPrice(), " OrderStopLoss:",OrderStopLoss());
         Sleep(RETRYDELAY);      
      }  
   }      
   return( false );
}

void doAlert(string alertStr)
{
      if (PopupAlert)
      {
         Alert(alertStr);
         Print(alertStr);
      }
      if (SoundAlert)
      {
         PlaySound(SoundFileName);
      }   
}

//return error message for each kind of errorCode
string ErrorDescription(int error_code)
{
    string error_string;

    switch( error_code ) {
        case 0:
        case 1:   error_string="no error";                                                  break;
        case 2:   error_string="common error";                                              break;
        case 3:   error_string="invalid trade parameters";                                  break;
        case 4:   error_string="trade server is busy";                                      break;
        case 5:   error_string="old version of the client terminal";                        break;
        case 6:   error_string="no connection with trade server";                           break;
        case 7:   error_string="not enough rights";                                         break;
        case 8:   error_string="too frequent requests";                                     break;
        case 9:   error_string="malfunctional trade operation (never returned error)";      break;
        case 64:  error_string="account disabled";                                          break;
        case 65:  error_string="invalid account";                                           break;
        case 128: error_string="trade timeout";                                             break;
        case 129: error_string="invalid price";                                             break;
        case 130: error_string="invalid stops";                                             break;
        case 131: error_string="invalid trade volume";                                      break;
        case 132: error_string="market is closed";                                          break;
        case 133: error_string="trade is disabled";                                         break;
        case 134: error_string="not enough money";                                          break;
        case 135: error_string="price changed";                                             break;
        case 136: error_string="off quotes";                                                break;
        case 137: error_string="broker is busy (never returned error)";                     break;
        case 138: error_string="requote";                                                   break;
        case 139: error_string="order is locked";                                           break;
        case 140: error_string="long positions only allowed";                               break;
        case 141: error_string="too many requests";                                         break;
        case 145: error_string="modification denied because order too close to market";     break;
        case 146: error_string="trade context is busy";                                     break;
        case 147: error_string="expirations are denied by broker";                          break;
        case 148: error_string="amount of open and pending orders has reached the limit";   break;
        case 149: error_string="hedging is prohibited";                                     break;
        case 150: error_string="prohibited by FIFO rules";                                  break;
        case 4000: error_string="no error (never generated code)";                          break;
        case 4001: error_string="wrong function pointer";                                   break;
        case 4002: error_string="array index is out of range";                              break;
        case 4003: error_string="no memory for function call stack";                        break;
        case 4004: error_string="recursive stack overflow";                                 break;
        case 4005: error_string="not enough stack for parameter";                           break;
        case 4006: error_string="no memory for parameter string";                           break;
        case 4007: error_string="no memory for temp string";                                break;
        case 4008: error_string="not initialized string";                                   break;
        case 4009: error_string="not initialized string in array";                          break;
        case 4010: error_string="no memory for array\' string";                             break;
        case 4011: error_string="too long string";                                          break;
        case 4012: error_string="remainder from zero divide";                               break;
        case 4013: error_string="zero divide";                                              break;
        case 4014: error_string="unknown command";                                          break;
        case 4015: error_string="wrong jump (never generated error)";                       break;
        case 4016: error_string="not initialized array";                                    break;
        case 4017: error_string="dll calls are not allowed";                                break;
        case 4018: error_string="cannot load library";                                      break;
        case 4019: error_string="cannot call function";                                     break;
        case 4020: error_string="expert function calls are not allowed";                    break;
        case 4021: error_string="not enough memory for temp string returned from function"; break;
        case 4022: error_string="system is busy (never generated error)";                   break;
        case 4050: error_string="invalid function parameters count";                        break;
        case 4051: error_string="invalid function parameter value";                         break;
        case 4052: error_string="string function internal error";                           break;
        case 4053: error_string="some array error";                                         break;
        case 4054: error_string="incorrect series array using";                             break;
        case 4055: error_string="custom indicator error";                                   break;
        case 4056: error_string="arrays are incompatible";                                  break;
        case 4057: error_string="global variables processing error";                        break;
        case 4058: error_string="global variable not found";                                break;
        case 4059: error_string="function is not allowed in testing mode";                  break;
        case 4060: error_string="function is not confirmed";                                break;
        case 4061: error_string="send mail error";                                          break;
        case 4062: error_string="string parameter expected";                                break;
        case 4063: error_string="integer parameter expected";                               break;
        case 4064: error_string="double parameter expected";                                break;
        case 4065: error_string="array as parameter expected";                              break;
        case 4066: error_string="requested history data in update state";                   break;
        case 4099: error_string="end of file";                                              break;
        case 4100: error_string="some file error";                                          break;
        case 4101: error_string="wrong file name";                                          break;
        case 4102: error_string="too many opened files";                                    break;
        case 4103: error_string="cannot open file";                                         break;
        case 4104: error_string="incompatible access to a file";                            break;
        case 4105: error_string="no order selected";                                        break;
        case 4106: error_string="unknown symbol";                                           break;
        case 4107: error_string="invalid price parameter for trade function";               break;
        case 4108: error_string="invalid ticket";                                           break;
        case 4109: error_string="trade is not allowed in the expert properties";            break;
        case 4110: error_string="longs are not allowed in the expert properties";           break;
        case 4111: error_string="shorts are not allowed in the expert properties";          break;
        case 4200: error_string="object is already exist";                                  break;
        case 4201: error_string="unknown object property";                                  break;
        case 4202: error_string="object is not exist";                                      break;
        case 4203: error_string="unknown object type";                                      break;
        case 4204: error_string="no object name";                                           break;
        case 4205: error_string="object coordinates error";                                 break;
        case 4206: error_string="no specified subwindow";                                   break;
        default:   error_string="unknown error";
    }

    return(error_string);
}